﻿using System;
using System.Web.UI.WebControls;
using System.Data;

public partial class NPS_app_MasterData_ReasonMaster : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrTitle.Text = "Master Data: Reason";
            loadForm();
            loadView();
            setPanel();
            pnlView.Visible = true;
        }
        else
        {
            if (pnlView.Visible)
            {
                loadView();
            }
        }
    }

    protected void loadForm()
    {
        Controller.NPSReasonCategoryController rcc = new Controller.NPSReasonCategoryController();
        ddlCCategory.DataSource = rcc.NPSReasonCategoryList;
        ddlCCategory.DataBind();
        ddlECategory.DataSource = rcc.NPSReasonCategoryList;
        ddlECategory.DataBind();
    }
    private void setPanel()
    {
        pnlView.Visible = false;
        pnlCreate.Visible = false;
        pnlEdit.Visible = false;
        pnlDetails.Visible = false;
        pnlDelete.Visible = false;
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void gv_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        loadData(e.CommandArgs.CommandArgument.ToString());
        if (e.CommandArgs.CommandName == "Editing")
        {
            goEdit();
        }
        else if (e.CommandArgs.CommandName == "Details")
        {
            goDetail();
        }
        else if (e.CommandArgs.CommandName == "Deleting")
        {
            goDelete();
        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        goNew();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        loadView();
    }
    protected void btnCSave_Click(object sender, EventArgs e)
    {
        Entities.NPSReason Reason = new Entities.NPSReason()
        {
            sxCreator = ICPUser.ID,
            iNPSReasonCategoryID = Convert.ToInt32(ddlECategory.SelectedValue),
            sxTitle = txtCTitle.Text,
            sxDetail = txtCDetail.Text
        };
        saveReason(Reason);
    }
    protected void btnCCancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnESave_Click(object sender, EventArgs e)
    {
        Entities.NPSReason marketingName = new Entities.NPSReason()
        {
            iID = getID,
            sxUpdater = ICPUser.ID,
            iNPSReasonCategoryID = Convert.ToInt16(ddlECategory.SelectedValue),
            sxTitle = txtETitle.Text,
            sxDetail = txtEDetail.Text
        };
        updateReason(marketingName);
    }
    protected void btnECancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDEdit_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDBack_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Entities.NPSReason marketingName = new Entities.NPSReason()
        {
            iID = getID,
            sxCreator = ICPUser.ID
        };
        deleteReason(marketingName);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        goView();
    }

    #region Audy's Custom Function

    protected int getID
    {
        get { return Convert.ToInt32(hfID.Value); }
    }
    protected void goView()
    {
        setPanel();
        pnlView.Visible = true;
        ltrTitle.Text = "Master Data Reason";
        txtCDetail.Text = string.Empty;
        txtCTitle.Text = string.Empty;
    }

    protected void goEdit()
    {
        setPanel();
        pnlEdit.Visible = true;
        ltrTitle.Text = "Master Data Reason Edit";
    }

    protected void goNew()
    {
        setPanel();
        pnlCreate.Visible = true;
        ltrTitle.Text = "Master Data Reason Create";
    }

    protected void goDetail()
    {
        setPanel();
        pnlDetails.Visible = true;
        ltrTitle.Text = "Master Data Reason Details";
    }

    protected void goDelete()
    {
        setPanel();
        pnlDelete.Visible = true;
        ltrTitle.Text = "Master Data Reason Delete";
    }

    protected void loadView()
    {
        Controller.NPSReasonController mnc = new Controller.NPSReasonController();
        GridView1.DataSource = mnc.DetailedNPSReasonList;
        GridView1.DataBind();
    }
    protected void loadData(string id)
    {
        //fetch data from DB
        Controller.NPSReasonController ANController = new Controller.NPSReasonController();
        Entities.NPSReason Reason = ANController.getNPSReason(Convert.ToInt32(id), true);

        //set all data to forms
        ddlECategory.SelectedValue = Reason.iNPSReasonCategoryID.ToString();


        txtETitle.Text = Reason.sxTitle;
        lblDT.Text = Reason.sxTitle;
        lblT.Text = Reason.sxTitle;

        txtEDetail.Text = Reason.sxDetail;
        lblDD.Text = Reason.sxDetail;
        lblD.Text = Reason.sxDetail;

        lblC.Text = Reason.oNPSReasonCategory.sxTitle;
        lblDR.Text = Reason.oNPSReasonCategory.sxTitle;

        lblID.Text = Reason.iID.ToString();
        lblDID.Text = Reason.iID.ToString();
        hfID.Value = Reason.iID.ToString();
    }
    protected void saveReason(Entities.NPSReason Reason)
    {
        Reason.sxCreator = ICPUser.ID;
        Controller.NPSReasonController ANController = new Controller.NPSReasonController();
        if (ANController.InsertNPSReasonData(Reason) == 0)
        { //return false; 
        }
        else
        { //return true;
            loadView();
        }
        goView();
        AlertNotification("Alasan " + Reason.sxTitle + " telah tersimpan");
    }

    protected void updateReason(Entities.NPSReason Reason)
    {
        Controller.NPSReasonController ANController = new Controller.NPSReasonController();
        ANController.UpdateNPSReasonData(Reason);
        loadView();
        goView();
        AlertNotification("Alasan " + Reason.sxTitle + " telah diperbaharui");
    }

    protected void deleteReason(Entities.NPSReason Reason)
    {
        Controller.NPSReasonController ANController = new Controller.NPSReasonController();
        ANController.DeleteNPSReasonData(Reason);
        loadView();
        goView();
        AlertNotification("Alasan " + Reason.sxTitle + " telah dihapus");
    }
    #endregion
}
